package com.ss.android.newmedia.j;

import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.utility.Logger;
import com.ss.android.common.AbsApiThread;
import com.ss.android.common.util.NetworkUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public final class a extends AbsApiThread {
    private final Handler a;
    private final String b;
    private final Context c;
    private final boolean d;

    public a(Context arg2, Handler arg3, boolean arg4) {

        this.a = arg3;
        this.c = arg2.getApplicationContext();
        this.b = arg2.getResources().getConfiguration().locale.getLanguage();
        this.d = arg4;
    }

    public final void run() {
        String v3_1;
        String v0_2;
        int v0_1;
        StringBuilder v3;
        StringBuilder v4;
        if (this.a != null) {
            int v1 = 18;
            try {
                v4 = new StringBuilder("http://ichannel.snssdk.com/service/2/app_alert/");
                v3 = v4.append("?has_market=");
                if (this.d) {
                    v0_1 = 1;
                } else {
                    v0_1 = 0;
                }

                v3.append(v0_1);
                v0_2 = this.b;
                if (!android.support.design.a.isTextEmpty(v0_2)) {
                    v4.append("&lang=").append(Uri.encode(v0_2));
                }

                try {
                    Object v0_4 = this.c.getSystemService(Context.TELEPHONY_SERVICE);
                    v3_1 = ((TelephonyManager) v0_4).getNetworkOperatorName();
                    if (!android.support.design.a.isTextEmpty(v3_1)) {
                        v4.append("&carrier=").append(Uri.encode(v3_1));
                    }

                    v0_2 = ((TelephonyManager) v0_4).getNetworkOperator();
                    if (!android.support.design.a.isTextEmpty(v0_2)) {
                        v4.append("&mcc_mnc=").append(Uri.encode(v0_2));
                    }

                    v4.append("&access=").append(NetworkUtils.getNetworkAccessType(this.c));
                } catch (Exception v0_3) {
                    Logger.w("AlertThread", "prepare app_alert param exception: " + v0_3);
                }
                Context v0_5 = this.c;
                if (v4.indexOf("#") > 0) {
                } else {
                    v3_1 = android.support.design.a.x != null ? android.support.design.a.x : android.support.design.a.v(v0_5.getPackageCodePath());
                    if (v3_1.length() == 0) {
                    } else {

                        v0_2 = v4.indexOf("?") > 0 ? "&" : "?";
                        v4.append(v0_2).append(v3_1);
                    }
                }
                try {
                    v0_2 = NetworkUtils.doGet(-1, v4.toString());
                    if (v0_2 != null && v0_2.length() != 0) {
                        JSONObject v3_2 = new JSONObject(v0_2);
                        if (!AbsApiThread.isApiSuccess(v3_2)) {
                            Logger.d("AlertThread", "get app_alert error: " + v3_2);
                            v0_1 = v1;
                        } else {
                            JSONArray v3_3 = v3_2.optJSONArray("datalist");
                            if (v3_3 != null && v3_3.length() > 0) {
                                ArrayList v1_1 = new ArrayList();
                                int v4_1 = v3_3.length();
                                for (v0_1 = 0; v0_1 < v4_1; ++v0_1) {
                                    JSONObject v2 = v3_3.optJSONObject(v0_1);
                                    if (v2 != null) {
                                        com.ss.android.newmedia.f.a v5 = new com.ss.android.newmedia.f.a();
                                        if (v5.a(v2)) {
                                            ((List) v1_1).add(v5);
                                        }
                                    }
                                }

                                Message v0_6 = this.a.obtainMessage(10003);
                                v0_6.obj = v1_1;
                                this.a.sendMessage(v0_6);
                                return;
                            }
                        }
                    }
                } catch (Throwable v0) {
                    Throwable v1_2 = v0;
                    v0_1 = TTUtils.getErrorCode(this.c, v1_2);
                    Logger.w("AlertThread", "get app_alert exception: " + v1_2);
                }
            } catch (Throwable v0) {
                Logger.e("AlertThread", "user growth SemUtils.updateUrl() error");
                v0_1 = v1;
            }
            Message v1_3 = this.a.obtainMessage(10004);
            v1_3.arg1 = v0_1;
            this.a.sendMessage(v1_3);
        }
    }
}

